<?php

class Model_SearchUser extends RL_Model2 {
	private $sstring = '';
	private $words = array();
	private $lpage = 1;
	private $pages = 0;

	private $max_words = 5;
	private $per_page = 5;

	public function setSearchString( $sstring = '' ) {
		if ( !empty($sstring) ) {
			mb_language('ru');
			mb_internal_encoding("UTF-8");
			mb_detect_order(array('UTF-8','WINDOWS-1251'));
			mb_regex_encoding('UTF-8');
			setlocale(LC_CTYPE, array("ru_RU.UTF-8","ru_RU","russian","ru_SU","ru"));
			
			$sstring = mb_strtolower($sstring);
      
			$sstring = mb_ereg_replace( '^\s+', '', $sstring );
			$sstring = mb_ereg_replace( '\s+$', '', $sstring );
			$sstring = mb_ereg_replace( '\s+', ' ', $sstring );
      
			$this->sstring = $sstring;
      
			$words = explode( ' ', $sstring );
			$words = array_flip($words);
			$words = array_flip($words);
      
			$words = array_chunk( $words, $this->max_words );
			$this->words = $words[0];
    }
  }
  
	public function setLPage( $lpage = 0 ) {
		$this->lpage = abs($lpage+0);
		if ( $this->lpage <= 0 ) {
			$this->lpage = 1;
		}
	}
  
	public function getLPage() {
		return $this->lpage;
	}
  
	public function getPages() {
		return $this->pages;
	}

	public function getWords() {
		return $this->words;
	}

  public function getItems() {
    $where = array();
    
    foreach ( $this->words as $word ) {
      $where[] = "(u.fullname like '%{$word}%' or u.status like '%{$word}%'" .
      " or u.openid_url like '%{$word}%')";
    }
    
    $where_string = ' and (' . join( ' or ', $where ) . ')';
    
    if ( isset($this->mm->aa->acl_data['comity_oid']) ) {
      $sql = "select count(*) as count " .
      " from {$this->store->db->db_prefix}class_user u " .
      " left join {$this->store->db->db_prefix}objects o on o.oid = u.oid " .
      " left join {$this->store->db->db_prefix}class_member m on u.oid = m.user_oid " .
      " where o.is_deleted = 0 and " .
      " m.community_oid = {$this->mm->aa->acl_data['comity_oid']} " .
      $where_string;
    } else {
      $sql = "select count(*) as count " .
      " from {$this->store->db->db_prefix}class_user u " .
      " left join {$this->store->db->db_prefix}objects o on o.oid = u.oid " .
      " where o.is_deleted = 0 " . $where_string;
    }
    
    $result = $this->store->query($sql);
		if ( !($result instanceof MysqlDBResult) ) {
			return 0;
		}
    
    $rec = $result->next();
    
  	$this->pages = (int) ($rec['count'] / $this->per_page);
		if ( $rec['count'] % $this->per_page > 0 ) {
			$this->pages++;
		}
		if ( $this->lpage > $this->pages && $this->pages > 0 ) {
			$this->lpage = $this->pages;
		}
    
    if ( isset($this->mm->aa->acl_data['comity_oid']) ) {
      $sql = "select u.oid, u.fullname, u.avatar, u.openid_url " .
      " from {$this->store->db->db_prefix}class_user u " .
      " left join {$this->store->db->db_prefix}objects o on o.oid = u.oid " .
      " left join {$this->store->db->db_prefix}class_member m on u.oid = m.user_oid " .
      " where o.is_deleted = 0 and " .
      " m.community_oid = {$this->mm->aa->acl_data['comity_oid']} " .
      $where_string .
      " limit " . (($this->lpage - 1) * $this->per_page) . ", {$this->per_page}";
    } else {
      $sql = "select u.oid, u.fullname, u.avatar, u.openid_url " .
      " from {$this->store->db->db_prefix}class_user u " .
      " left join {$this->store->db->db_prefix}objects o on o.oid = u.oid " .
      " where o.is_deleted = 0 " . $where_string .
      " limit " . (($this->lpage - 1) * $this->per_page) . ", {$this->per_page}";
    }
    
    //print $sql;exit;
    
    $result = $this->store->query($sql);
		if ( !($result instanceof MysqlDBResult) ) {
			return 0;
		}
    
    $items = array();
    while ( $row = $result->next() ) {
      $items[] = $row;
    }
    
    return $items;
  }
}